package com.zhongjinkang.rhythm.AppView.Fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding

abstract class BaseFragment<VB : ViewBinding,VM:ViewModel>(
    private val inflate: (LayoutInflater, ViewGroup?, Boolean) -> VB,
    private val viewModelClass:Class<VM>?,
    private val publicViewModelTarget: Boolean=false
) : Fragment() {
    protected lateinit var binding: VB;
   private val viewModel by lazy {
        val viewModelProvider=ViewModelProvider(this)
        viewModelClass?.let {
            viewModelProvider[it]
        }


   }

    public val  publicViewModel by lazy {
        if (publicViewModelTarget){

        }else{
            null
        }
    }
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding=inflate(inflater,container,false)
        initFragment(binding,viewModel,savedInstanceState)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        toRefreshUI(viewModel)
    }

    abstract fun  initFragment(binding: VB,viedModel:VM?, savedInstanceState: Bundle?)


    abstract fun toRefreshUI(viedModel: VM?)
}